//
//  LYLChargeEditorView.m
//  ONE
//
//  Created by dllo on 16/6/12.
//  Copyright © 2016年 GroupNine. All rights reserved.
//

#import "LYLChargeEditorView.h"

@interface LYLChargeEditorView ()

/** 编辑label */
@property (nonatomic, strong) UILabel *editorLabel;

/** 喜欢按钮 */
@property (nonatomic, strong) UIButton *likeButton;

/** 喜欢数label */
@property (nonatomic, strong) UILabel *likeNumLabel;

/** 更多按钮 */
@property (nonatomic, strong) UIButton *moreButton;

@end

@implementation LYLChargeEditorView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self createView];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self createView];
    }
    return self;
}

#pragma mark --创建编辑视图
- (void)createView
{
    if (_editorLabel) {
        return;
    }
    
    _moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_moreButton setImage:[UIImage imageNamed:@"more_normal"] forState:UIControlStateNormal];
    [_moreButton setImage:[UIImage imageNamed:@"more_highlighted"] forState:UIControlStateHighlighted];
    [_moreButton addTarget:self action:@selector(didClickedMoreButton) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_moreButton];
    [_moreButton makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(44);
        make.right.equalTo(self).offset(-8);
        make.centerY.equalTo(self);
    }];
    
    _likeNumLabel = [UILabel new];
    _likeNumLabel.textColor = LYLDarkGrayTextColor;
    _likeNumLabel.font = FontWithSize(12);
    [_likeNumLabel setContentHuggingPriority:251 forAxis:UILayoutConstraintAxisHorizontal];
    [self insertSubview:_likeNumLabel atIndex:2];
    [_likeNumLabel makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_moreButton.left).offset(-10);
        make.centerY.equalTo(self);
    }];
    
    _likeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_likeButton setImage:[UIImage imageNamed:@"like_normal"] forState:UIControlStateNormal];
    [_likeButton setImage:[UIImage imageNamed:@"like_selected"] forState:UIControlStateHighlighted];
    [_likeButton addTarget:self action:@selector(didClickedLikeButton) forControlEvents:UIControlEventTouchUpInside];
    [self insertSubview:_likeButton atIndex:3];
    [_likeButton makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(44);
        make.right.equalTo(_likeNumLabel.left);
        make.centerY.equalTo(self);
    }];
    
    _editorLabel = [UILabel new];
    _editorLabel.textColor = LYLDarkGrayTextColor;
    _editorLabel.font = FontWithSize(12);
    _editorLabel.numberOfLines = 0;
    [self addSubview:_editorLabel];
    [_editorLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(12);
        make.right.lessThanOrEqualTo(_likeButton.left).offset(8);
        make.centerY.equalTo(self);
    }];
}

#pragma mark --喜欢按钮点击方法
- (void)didClickedLikeButton
{
    if (_praiseBlock) {
        _praiseBlock();
    }
    NSLog(@"👍");
}

#pragma mark --更多按钮点击方法
- (void)didClickedMoreButton
{
    if (_moreBlock) {
        _moreBlock();
    }
    NSLog(@"更多");
}

#pragma mark --设置编辑页
- (void)configureViewWithEditorText:(NSString *)editorText praiseNum:(NSInteger)praiseNum praiseBlock:(CommonActionBlock)praiseBlock moreBlock:(CommonActionBlock)moreBlock
{
    _editorLabel.text = editorText;
    _likeNumLabel.text = [@(praiseNum) stringValue];
    _praiseBlock = praiseBlock;
    _moreBlock = moreBlock;
}

@end
